package com.px.plan.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.px.common.core.domain.entity.SysDictData;
import com.px.common.core.domain.entity.SysUser;
import com.px.device.service.IDroneInfoService;
import com.px.plan.domain.PlanActionUser;
import com.px.plan.mapper.PlanActionUserMapper;
import com.px.plan.service.IPlanActionUserService;
import com.px.system.domain.DroneInfo;
import com.px.system.service.ISysConfigService;
import com.px.system.service.ISysDictDataService;
import com.px.system.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 行动关联人员Service业务层处理
 *
 * @author WRJ
 * @date 2024-08
 */
@Service
public class PlanActionUserServiceImpl implements IPlanActionUserService
{
    @Autowired
    private PlanActionUserMapper planActionUserMapper;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private ISysUserService userService;
    @Autowired
    private IDroneInfoService droneInfoService;
    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询行动关联人员
     *
     * @param id 行动关联人员主键
     * @return 行动关联人员
     */
    @Override
    public PlanActionUser selectPlanActionUserById(Long id)
    {
        return planActionUserMapper.selectPlanActionUserById(id);
    }

    /**
     * 查询行动关联人员列表
     *
     * @param planActionUser 行动关联人员
     * @return 行动关联人员
     */
    @Override
    public List<PlanActionUser> selectPlanActionUserList(PlanActionUser planActionUser)
    {
        return planActionUserMapper.selectPlanActionUserList(planActionUser);
    }

    /**
     * 新增行动关联人员
     *
     * @param planActionUser 行动关联人员
     * @return 结果
     */
    @Override
    public int insertPlanActionUser(PlanActionUser planActionUser)
    {
        return planActionUserMapper.insertPlanActionUser(planActionUser);
    }

    /**
     * 修改行动关联人员
     *
     * @param planActionUser 行动关联人员
     * @return 结果
     */
    @Override
    public int updatePlanActionUser(PlanActionUser planActionUser)
    {
        return planActionUserMapper.updatePlanActionUser(planActionUser);
    }

    /**
     * 批量删除行动关联人员
     *
     * @param ids 需要删除的行动关联人员主键
     * @return 结果
     */
    @Override
    public int deletePlanActionUserByIds(Long[] ids)
    {
        return planActionUserMapper.deletePlanActionUserByIds(ids);
    }

    /**
     * 删除行动关联人员信息
     *
     * @param id 行动关联人员主键
     * @return 结果
     */
    @Override
    public int deletePlanActionUserById(Long id)
    {
        return planActionUserMapper.deletePlanActionUserById(id);
    }

    @Override
    public List<SysUser> selectUser() {
        // 飞手角色绑定
        String flyingHandIds = sysConfigService.selectConfigByKey("flyingHandIds");
        // 指挥角色绑定
//        String commandIds = sysConfigService.selectConfigByKey("commandIds");
        // 特勤角色绑定
        String secretServiceIds = sysConfigService.selectConfigByKey("secretServiceIds");

        Assert.hasLength(flyingHandIds,"请在系统参数设置中配置飞手角色!");
//        Assert.hasLength(commandIds,"请在系统参数设置中配置指挥角色!");
        Assert.hasLength(secretServiceIds,"请在系统参数设置中配置特勤角色!");

        String roleIds = String.join(",", flyingHandIds, secretServiceIds);
        roleIds.split(",");
        List<SysUser> users = userService.selectUserByRoleIds(roleIds.split(","));


        return users;
    }

    @Override
    public List<JSONObject> getFlyingInfo(Long actionId) {
        String flyingHandIds = sysConfigService.selectConfigByKey("flyingHandIds");
        Assert.hasLength(flyingHandIds,"请在系统参数设置中配置飞手角色!");

        return getInfoByDutyCodes(actionId,flyingHandIds);
    }

    private List<JSONObject> getInfoByDutyCodes(Long actionId, String flyingHandIds) {
        List<JSONObject> result = new ArrayList<>();
        List<PlanActionUser> users = selectUserByRoleIds(actionId, flyingHandIds.split(","));
        if (CollectionUtil.isEmpty(users)) {
            return new ArrayList<>();
        }
        List<DroneInfo> droneInfos = droneInfoService.selectDroneInfoByUserNames(users.stream().map(PlanActionUser::getUserName).collect(Collectors.toList()));

        Map<String, String> deviceTypeMap = new HashMap<>();
        SysDictData dictData = new SysDictData();
        dictData.setDictType("device_type");
        dictDataService.selectDictDataList(dictData).forEach(d-> deviceTypeMap.put(d.getDictValue(), d.getDictLabel()));


        users.forEach(u -> {
            JSONObject item = new JSONObject();
            JSONObject userJson = JSON.parseObject(JSON.toJSONString(u));
            item.putAll(userJson);
            if (CollectionUtil.isNotEmpty(droneInfos)) {
                DroneInfo d = droneInfos.stream().filter(drone -> StringUtils.equals(drone.getDutyCode(), u.getUserName())).findFirst().orElse(new DroneInfo());
                JSONObject droneJson = JSON.parseObject(JSON.toJSONString(d));
                droneJson.put("typeName", deviceTypeMap.getOrDefault(d.getType(), d.getType()));
                item.putAll(droneJson);
            }
            // 默认将所有设备设为下线,是否上线根据参与行动判断
            item.put("droneState", 0);
            result.add(item);
        });

        return result;
    }

    private List<PlanActionUser> selectUserByRoleIds(Long actionId, String[] roleIds) {
        return planActionUserMapper.selectUserByRoleIds(actionId, roleIds);
    }

    @Override
    public List<JSONObject> getSecretInfo(Long actionId) {
        String secretServiceIds = sysConfigService.selectConfigByKey("secretServiceIds");
        Assert.hasLength(secretServiceIds,"请在系统参数设置中配置特勤角色!");

        return getInfoByDutyCodes(actionId, secretServiceIds);
    }

    @Override
    public void deletePlanActionUserByActionId(Long actionId) {
        planActionUserMapper.deletePlanActionUserByActionId(actionId);
    }
}
